<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: thread.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>thread.h</h1><a href="thread_8h.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00006"></a>00006 <span class="preprocessor">#ifndef THREAD_H_</span>
<a name="l00007"></a>00007 <span class="preprocessor"></span><span class="preprocessor">#define THREAD_H_</span>
<a name="l00008"></a>00008 <span class="preprocessor"></span>
<a name="l00009"></a><a class="code" href="thread_8h.html#8f0f9587389901f5f0b1a5be4c6832d1">00009</a> <span class="preprocessor">#define NLB_THREADS_SIZE 2</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a><a class="code" href="thread_8h.html#e07c427473a6b2ff701b4f72702091eb">00011</a> <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> **<a class="code" href="thread_8h.html#e07c427473a6b2ff701b4f72702091eb">nlb_kthreads</a>[<a class="code" href="thread_8h.html#8f0f9587389901f5f0b1a5be4c6832d1">NLB_THREADS_SIZE</a>];
<a name="l00012"></a>00012 
<a name="l00016"></a><a class="code" href="thread_8h.html#6e122a678ab2f0b6f13a266c214813eb">00016</a> <span class="keywordtype">int</span> <a class="code" href="thread_8h.html#6e122a678ab2f0b6f13a266c214813eb" title="Cria threads para enviar e receber mensagens UDP.">nlb_thread_init</a>(<span class="keywordtype">void</span>)
<a name="l00017"></a>00017 {
<a name="l00018"></a>00018         <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> **new_thread;
<a name="l00019"></a>00019         <span class="keywordtype">int</span> idx = 0;
<a name="l00020"></a>00020         
<a name="l00021"></a>00021         nlb_kthreads[0] = <a class="code" href="heartbeat_8h.html#95053b46f210b2f1451e3314bd4e4221">hb_get_udp_server_thread</a>();
<a name="l00022"></a>00022         nlb_kthreads[1] = <a class="code" href="heartbeat_8h.html#61f03f1313c2bf412a60f54162b287da">hb_get_udp_client_thread</a>();
<a name="l00023"></a>00023         
<a name="l00024"></a>00024         <span class="keywordflow">for</span>(idx = 0; idx &lt; <a class="code" href="thread_8h.html#8f0f9587389901f5f0b1a5be4c6832d1">NLB_THREADS_SIZE</a>; idx++) 
<a name="l00025"></a>00025         {       
<a name="l00026"></a>00026                 new_thread = nlb_kthreads[idx]; 
<a name="l00027"></a>00027         
<a name="l00028"></a>00028             <span class="comment">/* start kernel thread */</span>
<a name="l00029"></a>00029             (*new_thread)-&gt;<a class="code" href="structksock__thread__t.html#34e131d5c7d4d36b7213114ebdf869c0" title="Thread onde o socket está executando.">thread</a> = kthread_run((<span class="keywordtype">void</span> *)(*new_thread)-&gt;fn, NULL, MODULE_NAME);
<a name="l00030"></a>00030             <span class="keywordflow">if</span> (IS_ERR((*new_thread)-&gt;thread)) 
<a name="l00031"></a>00031             {
<a name="l00032"></a>00032                     printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": unable to start kernel thread\n"</span>);
<a name="l00033"></a>00033                     kfree(*new_thread);
<a name="l00034"></a>00034                     *new_thread = NULL;
<a name="l00035"></a>00035                     <span class="keywordflow">return</span> -ENOMEM;
<a name="l00036"></a>00036             }
<a name="l00037"></a>00037         }
<a name="l00038"></a>00038 
<a name="l00039"></a>00039     <span class="keywordflow">return</span> 0;
<a name="l00040"></a>00040 }
<a name="l00041"></a>00041 
<a name="l00045"></a><a class="code" href="thread_8h.html#7c8900a7756e71bec1ed910214a1bfd9">00045</a> <span class="keywordtype">int</span> <a class="code" href="thread_8h.html#7c8900a7756e71bec1ed910214a1bfd9" title="Encerra os threads.">nlb_thread_exit</a>(<span class="keywordtype">void</span>)
<a name="l00046"></a>00046 {
<a name="l00047"></a>00047         <span class="keyword">struct </span><a class="code" href="structksock__thread__t.html" title="Thread usada para estabelecer conexões socket.">ksock_thread_t</a> **new_thread;
<a name="l00048"></a>00048         <span class="keywordtype">int</span> idx = 0;
<a name="l00049"></a>00049     <span class="keywordtype">int</span> err;
<a name="l00050"></a>00050 
<a name="l00051"></a>00051         <span class="keywordflow">for</span>(idx = 0; idx &lt; <a class="code" href="thread_8h.html#8f0f9587389901f5f0b1a5be4c6832d1">NLB_THREADS_SIZE</a>; idx++) 
<a name="l00052"></a>00052         {       
<a name="l00053"></a>00053                 new_thread = nlb_kthreads[idx];
<a name="l00054"></a>00054                 
<a name="l00055"></a>00055             <span class="keywordflow">if</span> ((*new_thread)-&gt;thread==NULL)
<a name="l00056"></a>00056                     printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": no kernel thread to kill\n"</span>);
<a name="l00057"></a>00057             <span class="keywordflow">else</span> 
<a name="l00058"></a>00058             {
<a name="l00059"></a>00059                     lock_kernel();
<a name="l00060"></a>00060                     err = kill_proc((*new_thread)-&gt;thread-&gt;pid, SIGKILL, 1);
<a name="l00061"></a>00061                     unlock_kernel();
<a name="l00062"></a>00062 
<a name="l00063"></a>00063                     <span class="comment">/* wait for kernel thread to die */</span>
<a name="l00064"></a>00064                     <span class="keywordflow">if</span> (err &lt; 0)
<a name="l00065"></a>00065                             printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": unknown error %d while trying to terminate kernel thread\n"</span>,-err);
<a name="l00066"></a>00066                     <span class="keywordflow">else</span> 
<a name="l00067"></a>00067                     {
<a name="l00068"></a>00068                             <span class="keywordflow">while</span> ((*new_thread)-&gt;running == 1)
<a name="l00069"></a>00069                                     msleep(10);
<a name="l00070"></a>00070                             printk(KERN_ALERT MODULE_NAME<span class="stringliteral">": succesfully killed kernel thread!\n"</span>);
<a name="l00071"></a>00071                     }
<a name="l00072"></a>00072             }
<a name="l00073"></a>00073 
<a name="l00074"></a>00074             <span class="comment">/* free allocated resources before exit */</span>
<a name="l00075"></a>00075             <span class="keywordflow">if</span> ((*new_thread)-&gt;sock != NULL) 
<a name="l00076"></a>00076             {
<a name="l00077"></a>00077                     sock_release((*new_thread)-&gt;sock);
<a name="l00078"></a>00078                     (*new_thread)-&gt;sock = NULL;
<a name="l00079"></a>00079             }
<a name="l00080"></a>00080 
<a name="l00081"></a>00081             kfree(*new_thread);
<a name="l00082"></a>00082             <a class="code" href="heartbeat_8h.html#9a09c37d118a488eb9427f761be51f08" title="Thread para o socket do servidor UDP.">hb_udp_server_thread</a> = NULL;
<a name="l00083"></a>00083         }
<a name="l00084"></a>00084     
<a name="l00085"></a>00085     printk(KERN_ALERT MODULE_NAME <span class="stringliteral">": modules unloaded\n"</span>);
<a name="l00086"></a>00086     <span class="keywordflow">return</span> 0;
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088 
<a name="l00089"></a>00089 
<a name="l00090"></a>00090 <span class="preprocessor">#endif </span><span class="comment">/*THREAD_H_*/</span>
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 23:05:55 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
